package com.blockmeta.bbs.immodule.activity.base;

import android.content.Context;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.chad.library.adapter.base.BaseQuickAdapter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author AirThor
 * @date 2020/3/5.
 */
public abstract class BaseIMSearchForF10AndMarketActivity extends BaseActivity implements TextWatcher, TextView.OnEditorActionListener {
    @BindView(R2.id.edt_search_input)
    public EditText mEdtSearchInput;
    @BindView(R2.id.tv_search_cancel)
    TextView mTvSearchCancel;
    @BindView(R2.id.tv_go_search)
    TextView mTvGoSearch;
    @BindView(R2.id.iv_clear_search)
    ImageView mIvClearSearch;
    @BindView(R2.id.group_go_search)
    public Group mGroupGoSearch;
    @BindView(R2.id.tv_search_result)
    public TextView mTvSearchResult;
    @BindView(R2.id.rcv_search_list)
    public RecyclerView mRcvSearchList;
    @BindView(R2.id.btn_send_share)
    public View mSendShare;

    private InputMethodManager mInputMethodManager;

    @Override
    protected boolean showTitle() {
        return false;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_base_im_search_for_f10_market, null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mEdtSearchInput.addTextChangedListener(this);
        mEdtSearchInput.setOnEditorActionListener(this);

        mRcvSearchList.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvSearchList.setAdapter(getAdapter());

        mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    /**
     * 由子类初始化列表适配器
     * @return 返回adapter
     */
    protected abstract BaseQuickAdapter getAdapter();

    @OnClick({R2.id.tv_search_cancel, R2.id.iv_clear_search, R2.id.tv_go_search,R2.id.btn_send_share})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_search_cancel) {
            finish();
        } else if (i == R.id.iv_clear_search) {
            mEdtSearchInput.setText("");
        } else if (i == R.id.tv_go_search) {
            String keyWord = mEdtSearchInput.getText().toString().trim();
            if (!TextUtils.isEmpty(keyWord)) {
                searchOnNet(keyWord);
            }
        }else if (i == R.id.btn_send_share){
            sendShare();
        }
    }

    /**
     * 发送分享
     */
    protected abstract void sendShare();

    /**
     * 搜索
     * @param content 搜索内容
     */
    protected abstract void searchOnNet(String content);

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (TextUtils.isEmpty(s.toString())){
            mGroupGoSearch.setVisibility(View.GONE);
            mIvClearSearch.setVisibility(View.GONE);
        }else {
            mIvClearSearch.setVisibility(View.VISIBLE);
            mGroupGoSearch.setVisibility(View.VISIBLE);
            mTvGoSearch.setText(String.format("搜索：\"%s\"", s.toString().trim()));
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean backValue = actionId == EditorInfo.IME_ACTION_SEARCH;
        if (backValue){
            String keyword = v.getText().toString().trim();
            if (TextUtils.isEmpty(keyword)){
                return true;
            }
            searchOnNet(keyword);
            hideSoftInput();
        }
        return backValue;
    }

    protected abstract class IMSubscriber<T> extends CommonSubscriber<T>{

        public IMSubscriber(Context context) {
            super(context);
        }

        public IMSubscriber(Context context, boolean showOtherException) {
            super(context, showOtherException);
        }


        @Override
        public void onComplete() {
            super.onComplete();
            netWorkComplete();
        }

        @Override
        public void onError(Throwable t) {
            super.onError(t);
            netWorkComplete();
        }
    }

    public void netWorkComplete() {
        if (getAdapter().getEmptyView() == null) {
            View emptyView = setEmptyView();
            if(emptyView!=null){
                getAdapter().setEmptyView(emptyView);
            }
        }
        mGroupGoSearch.setVisibility(View.GONE);
        mTvSearchResult.setVisibility(View.VISIBLE);
    }

    /**
     * 搜索数据为空的布局由子类返回
     * @return 空布局
     */
    protected abstract View setEmptyView();

    /**
     * 隐藏软件盘
     */
    protected void hideSoftInput() {
        mInputMethodManager.hideSoftInputFromWindow(mEdtSearchInput.getWindowToken(), 0);
    }
}
